page.pageInit = function(){
  //初始化grid
  MainGirdCtr.gridInit();
  MainGirdCtr.buttonBind()
  ChildGirdCtr.gridInit();
  ChildGirdCtr.buttonBind()
}

//grid控制器
var MainGirdCtr =  $.extend({},defaultGridConFig,{
   gridInit: function(){
	 var _this = this;
	//创建jqGrid组件
	_this.grid = $("#MainGrid").jqGrid({
		 			datatype: "json",
		 			url : contextPath+'/depot/material/query.htm',//组件创建完成之后请求数据的url
					datatype : "json",//请求数据返回的类型。可选json,xml,txt
					colModel : [ //jqGrid每一列的配置信息。包括名字，索引，宽度,对齐方式.....
					             {label:"物料编号", name : "materialId",sortable:false,width : 140,align : "center",key:true,hidden:true},
					             {label:"物料名称", name : 'materialName',sortable:false,width : 200,align : "center"}, 
					             {label:"物料品牌", name : 'materialBrand',sortable:false,width : 140,align : "center"}, 
					             {label:"物料类型", name : 'materialType',sortable:false,width : 80,align : "center",edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(_materialType)}}, 
					             {label:"单位", name : 'materialUnit',sortable:false,width : 80,align : "center",edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(_materialUnit)}}, 
					             {label:"保质期", name : 'materialLife',sortable:false,width : 80,align : "center",edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(_materialLife)}}, 
					             {label:"是否可序列化", name : 'isSerialize',sortable:false,width : 120,align : "center",edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(_yesOrNo)}}, 
					             {label:"状态", name : 'materialStatus',sortable:false,width : 80,align : "center",edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(systemDict.useStatus)}}, 
					             {label:"创建时间", name : 'createTime',sortable:false,width : 130,align : "center",formatter:DatFormatter}
					           ],
		            shrinkToFit:false,   
		            autoScroll: true, 
					rowNum : 10,//一页显示多少条
					rowList : [ 10, 20, 30 ],//可供用户选择一页显示多少条
					rownumWidth: 20, //设置显示序号的宽度，默认为25  
					pager : '#MainGridPager',//表格页脚的占位符(一般是div)的id
					sortname : 'create_Time',//初始化的时候排序的字段
					sortorder : "asc",//排序方式,可选desc,asc
					mtype : "post",//向后台请求数据的ajax的类型。可选post,get
					width:page.window.grid.width,
					height:page.window.grid.halfHeight,
					onSelectRow:function(rowId){
						MainGirdCtr.event.selectRow(_this,rowId);
					}
				});
		/*创建jqGrid的操作按钮容器*/
		/*可以控制界面上增删改查的按钮是否显示*/
		$("#MainGrid").jqGrid('navGrid', '#MainGridPager', {edit:false,del:false,add:false,search:false,refresh:false});
	},
	buttonBind:function(){//控制按钮初始化
	  var _this = this;
	  $("#addBtn").bind("click",function(){_this.event.doAdd(_this,this)});
	  $("#editBtn").bind("click",function(){_this.event.doEdit(_this,this)});
	  $("#deleteBtn").bind("click",function(){_this.event.doDelete(_this,this)});
	},
	event:{//事件
	  doAdd:function(_this,btn){
		  var menuId = $(btn).attr("menuId");
		  var params = {menuId:menuId}
		  showWindow("添加基础信息",contextPath+"/depot/material/addView.htm",params,function(){
			  MainGirdCtr.reLoad();
		  });
	  },
	  doEdit:function(_this,btn){//修改
		  var menuId = $(btn).attr("menuId");
		  var node = MainGirdCtr.getSelectRowData();
		  if(node){
			  var params = {menuId:menuId,id:node.materialId}
			  showWindow("修改基础信息",contextPath+"/depot/material/editView.htm",params,function(){
				  MainGirdCtr.reLoad();
			  });
		  }else{
			  layer.msg("选择需要修改的数据");
		  }
	  },
	  doDelete:function(_this,btn){//删除
		  var id = _this.grid.jqGrid('getGridParam','selrow');
		  if(id){
			 var laIndex = layer.confirm('确定要删除该数据吗？', {
				  btn: ['确定','取消'] //按钮
				}, function(){
					_this.event.gridDoDelete(id,_this);
					layer.close(laIndex);
				});
		  }else{
			  layer.msg("请选择删除的数据.");
		  }
	  },gridDoDelete:function(id,_this){
		  $.dataPost(contextPath+'/depot/material/delete.htm',{id:id},function(result){
			  if(result.success){
				  MainGirdCtr.reLoad();
				  TreeCtr.reloadSelectNode();
			  }else{
				  layer.msg(result.msg); 
			  }
		  });
	  },selectRow:function(_this,rowId){
		 var rowData = _this.grid.getRowData(rowId);
		 ChildGirdCtr.gridQuery({materialId:rowData.materialId})
	  }
	}
});


var ChildGirdCtr = $.extend({},defaultGridConFig,{
		   gridInit: function(){
			 var _this = this;
			//创建jqGrid组件
			_this.grid = $("#ChildGrid").jqGrid({
				 			datatype: "json",			
				 			url : contextPath+'/depot/property/query.htm',//组件创建完成之后请求数据的url
							datatype : "json",//请求数据返回的类型。可选json,xml,txt
							colModel : [ //jqGrid每一列的配置信息。包括名字，索引，宽度,对齐方式.....
							             {label:"主键", name : "propertyId",sortable:false,hidden:true,key:true},
							             {label:"物料编号", name : 'materialNo',sortable:false,width : 140,align : "center"}, 
							             {label:"物料子名称", name : 'materialPropertyName',sortable:false,width : 140,align : "center"}, 
							             {label:"助记码", name : 'mnemonicCode',sortable:false,width : 140,align : "center"}, 
							             {label:"规格", name : 'spec',sortable:false,width : 220,align : "center"}, 
							             {label:"颜色", name : 'color',sortable:false,width : 80,align : "center"},
							             {label:"状态", name : 'materialStatus',sortable:false,width : 80,align : "center",edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(systemDict.useStatus)}}, 
							             {label:"创建时间", name : 'createTime',sortable:false,width : 130,align : "center",formatter:DatFormatter} 
							           ],
				            shrinkToFit:false,   
				            autoScroll: true, 
							rowNum : 10,//一页显示多少条
							rowList : [ 10, 20, 30 ],//可供用户选择一页显示多少条
							rownumWidth: 20, //设置显示序号的宽度，默认为25  
							pager : '#ChildGridPager',//表格页脚的占位符(一般是div)的id
							sortname : 'create_Time',//初始化的时候排序的字段
							sortorder : "asc",//排序方式,可选desc,asc
							mtype : "post",//向后台请求数据的ajax的类型。可选post,get
							width:page.window.grid.width,
							height:page.window.grid.halfHeight
						});
				/*创建jqGrid的操作按钮容器*/
				/*可以控制界面上增删改查的按钮是否显示*/
				$("#ChildGrid").jqGrid('navGrid', '#ChildGridPager', {edit:false,del:false,add:false,search:false,refresh:false});
			},
			buttonBind:function(){//控制按钮初始化
			  var _this = this;
			  $("#addPropertyBtn").bind("click",function(){_this.event.doAdd(_this,this)});
			  $("#editPropertyBtn").bind("click",function(){_this.event.doEdit(_this,this)});
			  $("#deletePropertyBtn").bind("click",function(){_this.event.doDelete(_this,this)});
			  $("#setBtn").bind("click",function(){_this.event.doSet(_this,this)});
			},
			event:{//事件
			  doAdd:function(_this,btn){
				  var menuId = $(btn).attr("menuId");
				  var node = MainGirdCtr.getSelectRowData();
				  if(node){
					  var params = {menuId:menuId,materialId:node.materialId,materialName:node.materialName};
					  showWindow("添加属性分类",contextPath+"/depot/property/addView.htm",params,function(){
						  ChildGirdCtr.reLoad();
					  });
				  }else{
					  layer.msg("请选择基础信息.");
				  }
			  },
			  doEdit:function(_this,btn){//修改
				  var menuId = $(btn).attr("menuId");
	    		  var node = ChildGirdCtr.getSelectRowData();
	    		  if(node){
	    			  var params = {menuId:menuId,id:node.propertyId}
	    			  showWindow("修改属性分类",contextPath+"/depot/property/editView.htm",params,function(){
	    				  ChildGirdCtr.reLoad();
	    			  });
	    		  }else{
	    			  layer.msg("选择需要修改的数据");
	    		  }

			  },doSet:function(_this,btn){//修改
				  var menuId = $(btn).attr("menuId");
	    		  var node = ChildGirdCtr.getSelectRowData();
	    		  if(node){
	    			  var params = "?menuId="+menuId+"&propertyId="+node.propertyId;
	    			  openIframeWindow("设置库位",contextPath+"/depot/property/setView.htm"+params
	    					  ,{},function(){
	    				  ChildGirdCtr.reLoad();
	    			  },true);
	    		  }else{
	    			  layer.msg("选择需要修改的数据");
	    		  }
				 
			  },
			  doDelete:function(_this){//删除
				  var id = _this.grid.jqGrid('getGridParam','selrow');
				  if(id){
					 var laIndex = layer.confirm('确定要删除该数据吗？', {
						  btn: ['确定','取消'] //按钮
						}, function(){
							_this.event.gridDoDelete(id,_this);
							layer.close(laIndex);
						});
				  }else{
					  layer.msg("请选择删除的数据.");
				  }
			  },gridDoDelete:function(id,_this){
				  $.dataPost(contextPath+'/depot/property/delete.htm',{id:id},function(result){
					  if(result.success){
						  ChildGirdCtr.reLoad();
					  }else{
						  layer.msg(result.msg); 
					  }
				  });
			  }
			}
});




